package com.cn.aam.checaiduo.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.util.AttributeSet;

import java.util.ArrayList;

/**
 * monker on 2017/6/8.
 */

public class RandomTextView extends AppCompatTextView {
    //高位快
    public static final int FIRSTF_FIRST = 0;
    //高位慢
    public static final int FIRSTF_LAST = 1;
    //速度相同
    public static final int ALL = 2;
    //用户自定义速度
    public static final int USER = 3;
    //偏移速度类型
    private int pianyiliangTpye;

    //   滚动总行数 可设置
    private int maxLine = 3;
    //   当前字符串长度
    private int numLength = 0;
    //   当前text
    private String text;


    //滚动速度数组
    private int[] pianyilianglist;
    //总滚动距离数组
    private int[] pianyiliangSum;
    //滚动完成判断
    private int[] overLine;

    private Paint whiteLineP, redTextP, whiteTextP;
    //第一次绘制
    private boolean firstIn = true;
    //滚动中
    private boolean auto = false;

    //text int值列表
    private ArrayList<Integer> arrayListText;
    private int firstNoZeroIndex = -1;

    //控件单位等分后的宽度
    private float averageWith;

    //基准线
    private float baseline;

    private int measuredHeight;
    private int mWith, mHight;


    public RandomTextView(Context context) {
        super(context);
    }

    public RandomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public RandomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    //按系统提供的类型滚动
    public void setPianyilian(int pianyiliangTpye) {
        this.text = getText().toString();

        pianyiliangSum = new int[text.length()];
        overLine = new int[text.length()];
        pianyilianglist = new int[text.length()];
        switch (pianyiliangTpye) {
            case FIRSTF_FIRST:
                for (int i = 0; i < text.length(); i++) {
                    pianyilianglist[i] = 8 - i;
                }

                break;
            case FIRSTF_LAST:
                for (int i = 0; i < text.length(); i++) {
                    pianyilianglist[i] = 8 + i;
                }

                break;
            case ALL:
                for (int i = 0; i < text.length(); i++) {
                    pianyilianglist[i] = 8;
                }

                break;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mWith = MeasureSpec.getSize(widthMeasureSpec);
        mHight = MeasureSpec.getSize(heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    //自定义滚动速度数组
    public void setPianyilian(int[] list) {
        this.text = getText().toString();

        pianyiliangSum = new int[list.length];
        overLine = new int[list.length];
        pianyilianglist = list;


    }


    @Override
    protected void onDraw(Canvas canvas) {
        averageWith = mWith / 5;
        float textSize = averageWith * 0.9f;

        if (firstIn) {
            firstIn = false;
            whiteLineP = new Paint();
            whiteLineP.setColor(Color.WHITE);
            whiteLineP.setStrokeWidth(5);
            whiteLineP.setAntiAlias(true);

            whiteTextP = new Paint();
            whiteTextP.setColor(Color.WHITE);
            whiteTextP.setTextSize(textSize);
            whiteTextP.setAntiAlias(true);


            redTextP = new Paint();
            redTextP.setColor(Color.parseColor("#F1AAAD"));
            redTextP.setTextSize(textSize);
            redTextP.setAntiAlias(true);

            super.onDraw(canvas);

            measuredHeight = getMeasuredHeight();
            baseline = mHight * 0.65f;
            invalidate();
        }
        drawNumber(canvas);
    }


    //绘制
    private void drawNumber(Canvas canvas) {
        for (int j = 0; j < numLength; j++) {
            for (int i = 1; i < maxLine; i++) {
                if (i == maxLine - 1 && i * baseline + pianyiliangSum[j] <= baseline) {
                    pianyilianglist[j] = 0;
                    overLine[j] = 1;
                    int auto = 0;
                    for (int k = 0; k < numLength; k++) {
                        auto += overLine[k];
                    }
                    if (auto == numLength * 2 - 1) {
                        this.auto = false;
                        handler.removeCallbacks(task);
                        invalidate();
                    }
                }


                if (overLine[j] == 0) {
                    if (arrayListText.get(j).equals("0")) return;
                    drawLine(canvas, 0 + averageWith * j);
                    drawText(canvas, setBack(arrayListText.get(j), maxLine - i - 1) + "", j, averageWith * (0.3f + j), i * baseline + pianyiliangSum[j]);
                } else {
                    //定位后画一次就好啦
                    if (overLine[j] == 1) {
                        overLine[j]++;
                        drawLine(canvas, 0 + averageWith * j);
                        drawText(canvas, arrayListText.get(j)+"", j, averageWith * (0.3f + j), baseline);
                    }
                }
            }
        }
    }

    //设置上方数字0-9递减
    private int setBack(int c, int back) {

        if (back == 0) return c;

        back = back % 10;

        int re = c - back;

        if (re < 0) re = re + 10;

        return re;
    }

    //开始滚动
    public void start() {

        this.text = getText().toString();
        numLength = text.length();

        arrayListText = getList(text);

        if (!auto) {
            auto = true;
            handler.postDelayed(task, 17);
        }

    }

    public void setMaxLine(int l) {
        this.maxLine = l;
    }

    private ArrayList<Integer> getList(String s) {

        ArrayList<Integer> arrayList = new ArrayList<Integer>();

        for (int i = 0; i < s.length(); i++) {

            String ss = s.substring(i, i + 1);

            int a = Integer.parseInt(ss);

            arrayList.add(a);
        }
        for (int i = 0; i < arrayList.size(); i++) {
            if (arrayList.get(i) > 0) {
                //获取第一个不为0的数字所在位置，确保这个数字之后的0不用白色绘制
                firstNoZeroIndex = i;
                break;
            }

        }

        return arrayList;

    }

    ;


    private static final Handler handler = new Handler();

    public void destroy() {
        auto = false;
        handler.removeCallbacks(task);

    }

    private final Runnable task = new Runnable() {

        public void run() {
            if (auto) {
                handler.postDelayed(this, 20);

                for (int j = 0; j < numLength; j++) {
                    pianyiliangSum[j] -= pianyilianglist[j];
                }
                invalidate();
            }

        }
    };


    private void drawText(Canvas mCanvas, String text, int index, float x, float y) {
        if (y >= -measuredHeight && y <= 2 * measuredHeight) {
            if (TextUtils.equals("0", text + "")) {
                if (index > firstNoZeroIndex) {
                    mCanvas.drawText(text + "", x, y, redTextP);
                } else {
                    mCanvas.drawText(text + "", x, y, whiteTextP);
                }
            } else
                mCanvas.drawText(text + "", x, y, redTextP);
        }
        return;

    }

    private void drawLine(Canvas mCanvas, float x) {
        mCanvas.drawLine(x, 0, x, mHight, whiteLineP);

    }
}